package aries.utils;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import java.io.File;
import java.io.IOException;

public class Task {
    
    /**
     * Process.
     */
    private Process process;

    /**
     * Create a new task.
     * 
     * @param application
     * @throws IOException 
     */
    public Task(String application) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        this.process = runtime.exec(application);
    }
    
    /**
     * Create new tasks.
     * 
     * @param application
     * @throws IOException 
     */
    public Task(String[] application) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        this.process = runtime.exec(application);
    }
    
    /**
     * Create new tasks.
     * @TODO: Fixing this.
     * 
     * @param theClass
     * @throws IOException
     * @throws InterruptedException 
     */
    public Task(Class theClass) throws IOException, InterruptedException {
        String javaBin = Util.getInfo(Util.JAVA_HOME)+File.separator+"bin"+File.separator+"java";
        String classpath = Util.getInfo(Util.JAVA_CLASS_PATH);
        String className = theClass.getCanonicalName();
        ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
        this.process = builder.start();
    }
    
    /**
     * Get the process.
     * 
     * @return Process
     */
    public Process getProcess() {
        return this.process;
    }
    
    /**
     * Kill the task.
     */
    public void kill() {
        this.process.destroy();
    }
    
}